package com.runfast.waimai.web.controller.cuser;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastGoodsSellRecordService;
import com.runfast.waimai.service.WaiMaiRunfastCuserService;
import com.runfast.waimai.service.WaiMaiRunfastCuseraddressService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * ${DESCRIPTION}  客户管理
 *
 * @author chenzheng
 * @create 2018-05-09 12:11
 **/
@RestController("cuserController")
@RequestMapping("/server/cuser")
public class CuserController {
    @Autowired
    private WaiMaiRunfastCuserService runfastCuserService;
    @Autowired
    private RunfastGoodsSellRecordService runfastGoodsSellRecordService;
    @Autowired
    private WaiMaiRunfastCuseraddressService waiMaiRunfastCuseraddressService;
    /**
     * 客户列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable,WaiMaiRunfastCuser cuser){
        Map<Object,Object> map1=new HashMap<Object, Object>();
        if(cuser!=null){
            if(StringUtils.isNotBlank(cuser.getUserMobile()) ){
                map1.put("userMobile",cuser.getUserMobile());
            }
            if(cuser.getOrderStatus()!=null){
                map1.put("orderStatus",cuser.getOrderStatus());
            }
            if(cuser.getLoadStatus()!=null){
                map1.put("loadStatus",cuser.getLoadStatus());
            }
        }
        map1.put("pageable",pageable);
        List<WaiMaiRunfastCuser> list=runfastCuserService.selectByMapWithPageable(map1);
        Integer total=runfastCuserService.countByMap(map1);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }



    /**用户订单列表*/
    @GetMapping("/cuserOrderList")
    public Result  cuserOrderList(Integer userid,@PageableDefault Pageable pageable){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        RunfastBusinessAccounts accounts = SessionContext.getRunfastBusinessAccounts();
        Map<Object,Object> map=new HashMap<Object, Object>();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        if(admin!=null && admin.getType()==3 && accounts!=null){
            //商家帐号登录----只能看自己商家的
            map1.put("businessId",accounts.getBusinessid());
        }else if(admin!=null  &&admin.getType()==2  && agentAccounts!=null){
            //代理商登录
            map1.put("agentIds",agentAccounts.getAgentid());
        }else if(admin !=null && admin.getType()==1){
            //后台管理员---查看全部的
        }
        map1.put("pageable",pageable);
        map1.put("userid",userid);
        List<RunfastGoodsSellRecord> list=runfastGoodsSellRecordService.selectByMapWithPageable(map1);
        Integer total=runfastGoodsSellRecordService.countByMap(map1);
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }


    /**售后订单列表*/


    /**客户收货地址*/
    @GetMapping("/cuserAddressList")
    public Result  cuserAddressList(Integer userid,@PageableDefault Pageable pageable){
        Map<Object,Object> map=new HashMap<Object, Object>();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        map1.put("pageable",pageable);
        map1.put("userid",userid);
        List<WaiMaiRunfastCuseraddress> list=waiMaiRunfastCuseraddressService.selectByMapWithPageable(map1);
        Integer total=waiMaiRunfastCuseraddressService.countByMap(map1);
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**修改密码*/
    @GetMapping("/setPassword")
    public Result setPassword(Integer id,String password){
        if(id!=null && StringUtils.isNotBlank(password)){
            WaiMaiRunfastCuser  runfastCuser=runfastCuserService.selectByPrimaryKey(id);
            if(runfastCuser!=null){
                runfastCuser.setPassword(password);
                runfastCuserService.updateByPrimaryKeySelective(runfastCuser);
            }else{return Result.fail(null,"找不到对应的客户！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**
     * 商品分类列表
     */
    /*@GetMapping("/select")
    @RequiresPermissions("server:goodsSellType:select")
    public Result select(){
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        return Result.ok(null,list);
    }*/



    /**
     * 修改商品分类
     */
   /* @PostMapping("/update")
    @RequiresPermissions("server:goodsSellType:update")
    public Result update(RunfastGoodsSellType type){
        runfastGoodsSellTypeService.updateByPrimaryKeySelective(type);
        return Result.ok(null);
    }*/

    /**
     * 删除角色---暂时不实现
     */
    /*@PostMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public R delete(@RequestBody Long[] roleIds){
        sysRoleService.deleteBatch(roleIds);

        return R.ok();
    }*/


}
